<%@page import="com.crm.dao.SourcesDAO,com.crm.dao.OpportunityDAO,com.crm.dao.PartSourcesDAO,com.crm.entity.Sources,com.crm.entity.Opportunity,com.crm.entity.Parts,com.crm.entity.Business"%>
<%@page import="java.util.List,java.util.*,com.crm.entity.PartSources" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Send Mail PopUp</title>

<link href="<%=application.getContextPath()%>/css/css-crm.css" rel="stylesheet" type="text/css" />

<script src="<%=application.getContextPath()%>/js/jquery-1.6.min.js"></script>

<script type="text/javascript">
$(function(){
	var items = $("#sourceid option");
	//alert(items.length);
	if(items.length<1){
		window.close();
	}
});
 function getFile(){
   document.getElementById("upfile").click();
 }
 function sub(obj){
    var file = obj.value;
    var fileName = file.split("\\");
    document.getElementById("attfile").innerHTML = fileName[fileName.length-1];
    document.myForm.submit();
    event.preventDefault();
  }
</script>
</head>

<body>
<div id="main">
 <div id="header">
    <div class="logo"><img src="<%=application.getContextPath() %>/images/logo.jpg" /></div>
	<div id="resultMsg" class="result_msg" >
	If You can not See The Source Names,Please Press CTRL+F5.
	</div>
  </div>
 <div id="content">
 <div id="resultMsg" class="result_msg" >
  <%
  	if(request.getAttribute("msg")!=null){
  		out.println(request.getAttribute("msg"));
  	}
  %>
  </div>
  <div class="work-area-re">
    <div id="savediv">
	<form id="companyForm" action="<%=application.getContextPath()%>/exportsource/sendMail.do" enctype="multipart/form-data" method="post">
    <% 
        String refno=request.getParameter("reflist");
    if(refno==null){
    	refno=request.getAttribute("reflist").toString();
    }
        System.out.println("reference Number Is:"+refno);
        %>
	  <input type="hidden" value="<%=refno%>" name="refno" id="refid"/>
	  <div id="container">
      <div class="box-re">
        <table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="6%">&nbsp;</td>
            <td width="25%">&nbsp;</td>
            <td width="37%">&nbsp;</td>
            <td width="9%">&nbsp;</td>
            <td width="21%">&nbsp;</td>
            <td width="2%">&nbsp;</td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td>To 
            <select id="sourceid" name="sourceName" class="">
            <option value="">Please Select</option>
            <%
            Business bus=(Business)session.getAttribute("business");
            System.out.println("Business Is:"+bus);
            int busid=bus.getBusinessId();
            //OpportunityDAO oppDao=new OpportunityDAO();
           	Opportunity opp=OpportunityDAO.getSingleInstance().getByRef(refno,busid);
            int refid=opp.getId();
            String refnum=opp.getRefNum();
            System.out.println("Reference Id Is:"+refid);
        	SourcesDAO sodao=new SourcesDAO();
        	PartSourcesDAO psdao=new PartSourcesDAO();
    	Set<String> so=new HashSet<String>();
        	List<PartSources> es=psdao.getSourcesByEnqid(refid,busid);
        	if(es!=null){
        		for(PartSources eSrc:es){
        			int srcId=eSrc.getSourceid();
    				Sources s=sodao.findById(srcId,busid);
    				so.add(s.getSourceName());
        		}
				String soNa=new String();
				if(request.getAttribute("soName")!=null){
				 soNa=request.getAttribute("soName").toString();
				 System.out.println("Source Name From Controller Is:"+soNa);
				}
        	
			Iterator<String> it=so.iterator();
			while(it.hasNext()){
				String s=it.next();
            %>
            <option <%if(soNa!=null && soNa.equals(s)){%> selected="selected" <%} %>value="<%=s%>"><%=s%></option>     
          <%} }%></select><br /></td>
            <td>Subject &nbsp; <input name="subject" type="text" class="form-sub" /></td>
            <td>Attachment</td>
            <td><div class="upload" id="attfile" onclick="getFile()">Click to upload a file</div>
						<div style='height: 0px;width: 0px; overflow:hidden;'>
                      <input id="upfile" type="file" value="upload" onchange="sub(this)" name="attachment"/>
                    </div></td>
            <td>&nbsp;</td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
          </tr>
          <tr>
            <td>Reference Number</td>
            <td><input type="text" name="enqnum" value="<%=refnum%>"/></td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
            <td>&nbsp;</td>
          </tr>
        </table>
      </div>
      
	   
	  <div class="box-re">
	  <table id="extab" width="100%" border="0"  class="mytable"  cellpadding="4" cellspacing="1" bgcolor="#C5E4EB">
          <tr>
            <td width="27" height="30" align="left" valign="middle" background="<%=application.getContextPath()%>/images/table-box.jpg"  style="background-repeat:repeat-x">S.No</td>
            <td width="242" align="left" valign="middle" background="<%=application.getContextPath()%>/images/table-box.jpg"   style="background-repeat:repeat-xbgcolor="#FFFFFF"> Part Number </td>
            
            <td width="216" align="left" valign="middle" background="<%=application.getContextPath()%>/images/table-box.jpg"   style="background-repeat:repeat-xbgcolor="#FFFFFF">Required Qty</td><td width="216" align="left" valign="middle" background="images/table-box.jpg"   style="background-repeat:repeat-xbgcolor="#FFFFFF">Make</td>
           <td width="216" align="left" valign="middle" background="<%=application.getContextPath()%>/images/table-box.jpg"   style="background-repeat:repeat-xbgcolor="#FFFFFF">Description</td><td width="216" align="left" valign="middle" background="images/table-box.jpg"   style="background-repeat:repeat-xbgcolor="#FFFFFF">Specification</td> 
            
             </tr> 
          
          <tbody id="tbodyid">
         
           <%  
    if(request.getAttribute("partsList")!=null){ 
    	List<Parts> lp=(List<Parts>)request.getAttribute("partsList");
    	 
    	System.out.println(lp); 
    	for(int i=0;i<lp.size();i++){
    	%>
          <tr>
          <td bgcolor="#FFFFFF">
            <input name="sno" type="text" value="<%=i+1 %>" size="5" />
            </td>
            <td align="left" valign="top" bgcolor="#FFFFFF">
            <input type="text" value="<%=lp.get(i).getPrtnum()%>" name="part<%=i+1 %>"/></td>
            <td align="left" valign="top" bgcolor="#FFFFFF">
            <input type="text" value="<%=lp.get(i).getQty() %>" name="quantity<%=i+1 %>" style="width: 64px; "/></td>
            <td align="left" valign="top" bgcolor="#FFFFFF">
            <input type="text" value="<%=lp.get(i).getMake() %>" name="make<%=i+1 %>"/>
            </td>
            <td align="left" valign="top" bgcolor="#FFFFFF">
            <input type="text" value="<%=lp.get(i).getDescription() %>" name="description<%=i+1 %>" style="width: 131px; "/></td>
            <td align="left" valign="top" bgcolor="#FFFFFF">
           <input type="text" value="<%=lp.get(i).getSpecification() %>" name="specification<%=i+1 %>" style="width: 94px; "/></td>
            
          </tr>
       
         <%} %> 
         
         <input type="hidden" value="<%=lp.size() %>" name="rowCount"/>
         <%        }
          %>
         
                </tbody>
        </table>
		
		
		</div>
	  <div class="box-re">
	  <input type="reset" class="button" value="Cancel" onclick="javascript:window.close();"/>
	  <input type="submit" class="button" value="Send Mail"/>
    	</div>
    	</div>
    </form>
    </div>
    </div>
   <div id="result"><div id="ajaxLoader" style="display:none" align="center"><img src="<%=application.getContextPath()%>/images/ajax_loader.gif"/></div></div>  
  </div>
  <div id="footer"></div>  
</div>
</body>
</html>
<script type="text/javascript">
$("#sourceid").change(function(){
	var sourceid=$("#sourceid").val();
	var refno=$("#refid").val();
	
	if(refno!=null || refno!=""){
	$("#ajaxLoader").show();
	$.ajax({
		url:"<%=application.getContextPath()%>/exportsource/setMail.do" ,
	  	data:"sourceid="+sourceid+"&refno="+refno,
	  	success:function(res){
	  		$("#container").remove();
	  		$("#header").remove();
	  		$('#result').html(res);
	  	}
	  }).done(function(html){
			$("#ajaxLoader").hide();
			//alert("Ajax Loading Completed");
		}).fail(function(html){
			$("#ajaxLoader").hide();
			alert("Failed To Load the Data. Please Press Ctrl+F5 And Try Again.");
		});
	}
});

</script>
